package com.example.pinkcat.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.getData.GetData;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class SuggestPage extends AppCompatActivity {
    Context context=this;
    Spinner spinner;
    EditText suggest;
    EditText mail;
    String type;
    List<String> list_type = new ArrayList<String>();

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Objects.requireNonNull(getSupportActionBar()).hide();//去掉标题栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_suggest_page);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }//深色信息栏
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);//允许此线程内发起网络请求

        spinner=(Spinner) findViewById(R.id.suggest_spinner);
        suggest=findViewById(R.id.suggest_context);
        mail=findViewById(R.id.mail);
        list_type.add("使用问题");
        list_type.add("app建议");
        list_type.add("网络问题");
        list_type.add("UI问题");
        list_type.add("其他问题");
        type=list_type.get(0);



        listen();//监听btn
        setSpanner();//设置下拉框
    }

    void listen(){
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        findViewById(R.id.sendbtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.progressBar_wait).setVisibility(View.VISIBLE);
                if (suggest.getText().toString().length()<2||mail.getText().toString().length()<2){
                    Toast.makeText(context,"请完善反馈信息",Toast.LENGTH_SHORT).show();
                    findViewById(R.id.progressBar_wait).setVisibility(View.INVISIBLE);
                    return;
                }
                if (GetData.suggest(type,suggest.getText().toString(),mail.getText().toString(),context)){
                    findViewById(R.id.progressBar_wait).setVisibility(View.INVISIBLE);
                    onBackPressed();
                }
                findViewById(R.id.progressBar_wait).setVisibility(View.INVISIBLE);
            }
        });

        //下拉框选择监听
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                type=list_type.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


    void setSpanner(){
        ArrayAdapter<String> spinner_adapter=new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,list_type);
        spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinner_adapter);
    }
}